package com.jinhao.crowd.service.impl;

import com.jinhao.crowd.entity.Menu;
import com.jinhao.crowd.entity.MenuExample;
import com.jinhao.crowd.exception.MenuNameRepeatException;
import com.jinhao.crowd.mapper.MenuMapper;
import com.jinhao.crowd.service.api.MenuService;
import com.jinhao.crowd.util.CrowdConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created on 2021/10/1.
 *
 * @author Valar Morghulis
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<Menu> getAll() {
        List<Menu> menuList = menuMapper.selectByExample(new MenuExample());
        return menuList;
    }

    @Override
    public void saveMenu(Menu menu) {
        try {
            menuMapper.insertSelective(menu);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MenuNameRepeatException(CrowdConstant.MESSAGE_MENUNAME_REPEAT);
        }
    }

    @Override
    public void updateMenu(Menu menu) {
        try {
            menuMapper.updateByPrimaryKey(menu);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MenuNameRepeatException(CrowdConstant.MESSAGE_MENUNAME_REPEAT);
        }
    }

    @Override
    public void removeMenu(Integer id) {
        menuMapper.deleteByPrimaryKey(id);
    }
}
